# coding=utf-8
# -*- coding: utf-8 -*-
# flake8: noqa
from qiniu import Auth, put_file, etag,put_data
import qiniu.config

#需要填写你的 Access Key 和 Secret Key
access_key = 'DxOgyZO5rmLN8cjS64oBF30WrV1nIaT-KmGXY9ZY'
secret_key = 'm1rQC5YGTvctbTAUwLJ4oLVO8y7LNRJBzVs6MmAX'

def image_storage(image_data):
    #构建鉴权对象
    q = Auth(access_key, secret_key)

    #要上传的空间
    bucket_name = 'info66'

    #上传后保存的文件名,如果自己去维护比较蛮烦,如果设置为None那么就交给七牛云维护
    # key = 'haha.png'
    key = None

    #生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)

    #要上传文件的本地路径
    # localfile = './33.jpg'
    # ret, info = put_file(token, key, localfile)
    ret, info = put_data(token, key, image_data)

    #如果上传成功之后返回图片名字
    if info.status_code == 200:
        return ret.get("key")
    else:
        return None

    # print(info)
    # print(ret)

if __name__ == '__main__':
    with open('./33.jpg','rb') as f:
        image_name = image_storage(f.read())
        print(image_name)